/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package humanNetwork;

/**
 *
 * @author jamie
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.FileUtils;
import utils.Handle;

/**
 *
 * @author jamiemacpherson
 */
public class HumanPPINetwork extends Network{

    public final static int ALL=0;
    public final static int HPRD=1;
    public final static int BIOGRID=2;

    private File edgeFile = new File("/Users/jamie/pfizer/HCV/data/scripps/brainarray/networkAnalysis/dataFiles/edges_gIds.dat");
    private File biogridFile = new File("/Users/jamie/Documents/work/bigDataFiles/biogrid/biogrid_hs.txt");
    private File hprdFile = new File("/Users/jamie/pfizer/HCV/data/scripps/brainarray/networkAnalysis/dataFiles/hprd.dat");


    public HumanPPINetwork(int type) {
        super();
        if(type==HPRD){
            setHprdFile();
            setNetwork(hprdFile);
        } if(type==BIOGRID){
            setBiogridFile();
            setNetwork(biogridFile);
        }else {
            setNetwork(edgeFile);
        }
    }

    private void setHprdFile() {
        Handle h = utils.Handle.getConnectedHandle("interactions");
        String sql = "select geneId_1, geneId_2 from interactions.hprd";
        ResultSet rs = h.runSQLquery(sql);
        BufferedWriter writer = FileUtils.getBufferedWriter(hprdFile);
        try {
            while (rs.next()) {
                writer.append(rs.getString("geneId_1")+"\t"+rs.getString("geneId_2")+"\n");

            }
            writer.close();
        } catch (Exception ex) {
            Logger.getLogger(HumanPPINetwork.class.getName()).log(Level.SEVERE, null, ex);
        }
        h.closeConnection();
    }

    private void setBiogridFile() {
        BufferedReader reader = FileUtils.getBufferedReader(new File("/Users/jamie/Documents/work/bigDataFiles/biogrid/BIOGRID-ORGANISM-Homo_sapiens-3.1.76.tab2.txt"));
        BufferedWriter writer = FileUtils.getBufferedWriter(biogridFile);
        try {
            String line;
            while ((line=reader.readLine()) != null) {
                String split[] = line.split("\t");
                Integer id1 = null;
                Integer id2 = null;
                try{
                    id1 = Integer.parseInt(split[1]);
                } catch(NumberFormatException ex){

                }
                try{
                    id2 = Integer.parseInt(split[2]);
                } catch(NumberFormatException ex){

                }
                if(id1!=null && id2!=null){
                   writer.append(id1+"\t"+id2+"\n") ;
                }


            }
            reader.close();
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(HumanPPINetwork.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
